<?php
    $xtpl_list      = new XTemplate("view/comment/list.htm");
    
    $title  = "Thông tin bình luận";
    $searchBy   = $_POST['searchby'];
    $offset     = $_GET['p'];
    $limit      = 5;
    $offset     = (isset($offset) && ($offset > 0)) ? $offset : 0;
    
    $keyword    = $_POST['keyword'];
   	$keyword	= str_replace(" ", "%", $keyword);
   	$keyword 	= "LIKE '%" . $keyword . "%'";
    switch($searchBy){
        case    1   :   $condition  = "product WHERE milkname $keyword"; break;
        case    2   :   $condition  = "member WHERE username $keyword"; break;
        default     :   $condition  = "product WHERE milkname $keyword"; break;
    }
    
    if(isset($_POST['submit'])){
        $totalRecords   = 0;
        if($_POST['keyword']!=NULL){
            $query  = "SELECT * FROM $condition";
            $re     = mysql_query($query);
            $rows   = mysql_fetch_array($re);
            
            if($searchBy == 1){
                $sql    = "SELECT * FROM comment WHERE product_id='" . $rows['id_p'] . "'";
                $result = mysql_query($sql);       
                while($row = mysql_fetch_array($result)){
                    $sql_1  = "SELECT * FROM product WHERE id_p='" . $row['product_id'] . "'";
                    $re_1   = mysql_query($sql_1);
                    $row_1  = mysql_fetch_array($re_1);
                    
                    $sql_2  = "SELECT * FROM member WHERE id_m='" . $row['user_id'] . "'";
                    $re_2   = mysql_query($sql_2);
                    $row_2  = mysql_fetch_array($re_2);
                    
                    $sql_date   = strtotime($row['date']);
                    $date       = date('d-m-Y H:i:s', $sql_date);
                    
                    $xtpl_list  -> assign('milkname', $row_1['milkname']);
                    $xtpl_list  -> assign('username', $row_2['username']);
                    $xtpl_list  -> assign('comment_date', $date);
                    $xtpl_list  -> assign('id', $row['id_cm']);
                    $xtpl_list  -> insert_loop('LIST.TABLES', array('LIST'=>$row));  
                    $totalRecords++;       
                }
            }
            if($searchBy == 2){
                $sql    = "SELECT * FROM comment WHERE user_id='" . $rows['id_m'] . "'";
                $result = mysql_query($sql);       
                while($row = mysql_fetch_array($result)){
                    $sql_1  = "SELECT * FROM product WHERE id_p='" . $row['product_id'] . "'";
                    $re_1   = mysql_query($sql_1);
                    $row_1  = mysql_fetch_array($re_1);
                    
                    $sql_2  = "SELECT * FROM member WHERE id_m='" . $row['user_id'] . "'";
                    $re_2   = mysql_query($sql_2);
                    $row_2  = mysql_fetch_array($re_2);
                    
                    $sql_date   = strtotime($row['date']);
                    $date       = date('d-m-Y H:i:s', $sql_date);
                    
                    $xtpl_list  -> assign('milkname', $row_1['milkname']);
                    $xtpl_list  -> assign('username', $row_2['username']);
                    $xtpl_list  -> assign('comment_date', $date);
                    $xtpl_list  -> assign('id', $row['id_cm']);
                    $xtpl_list  -> insert_loop('LIST.TABLES', array('LIST'=>$row)); 
                    $totalRecords++;
                }          
            }   
        }
        else{
            $error  = "Bạn chưa điền từ khóa";
            $xtpl_list  -> assign('error', $error);   
        }
    }
    else{
        $query          = "SELECT count(*) AS totalRecords FROM comment";
        $re             = mysql_query($query);
        $rows           = mysql_fetch_assoc($re);
        $totalRecords   = $rows['totalRecords'];
        $pages      = pagging($totalRecords, $offset, $limit, '?page=comment');
        $sql        = "SELECT * FROM comment ORDER BY id_cm DESC LIMIT " . $offset . ", " . $limit . "";
        $result     = mysql_query($sql);
        while($row = mysql_fetch_array($result)){
            $sql_1  = "SELECT * FROM product WHERE id_p='" . $row['product_id'] . "'";
            $re_1   = mysql_query($sql_1);
            $row_1  = mysql_fetch_array($re_1);
                        
            $sql_2  = "SELECT * FROM member WHERE id_m='" . $row['user_id'] . "'";
            $re_2   = mysql_query($sql_2);
            $row_2  = mysql_fetch_array($re_2);
            
            $sql_date   = strtotime($row['date']);
            $date       = date('d-m-Y H:i:s', $sql_date);
                        
            $xtpl_list  -> assign('milkname', $row_1['milkname']);
            $xtpl_list  -> assign('username', $row_2['username']);
            $xtpl_list  -> assign('comment_date', $date);
            $xtpl_list  -> assign('id', $row['id_cm']);
            $xtpl_list  -> insert_loop('LIST.TABLES', array('LIST'=>$row));
        }
    }
    
    if(isset($_POST['delete'])){
        if(is_array($_POST['check'])){
            $list_id    = join($_POST['check'], ',');
            $sql    = "DELETE FROM comment WHERE id_cm IN (" . $list_id . ")";
            $result = mysql_query($sql);
            header("location:index.php?page=comment");
        }
    }
    
    $xtpl_list  -> assign('total', $totalRecords);
    $xtpl_list  -> assign('pages', $pages);
    $xtpl_list      -> parse("LIST");
    $content        = $xtpl_list -> text("LIST");
?>